<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggleDialog" :class="{ active }">
      <span class="placeholder" v-if="active">请选择配送地址</span>
      <span class="placeholder" v-else>{{ fullLocation }}</span>
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <div v-if="loading" class="loading"></div>
      <template v-else>
        <span
          @click="changeItem(item)"
          class="ellipsis"
          v-for="item in currList"
          :key="item.code"
          >{{ item.name }}</span
        >
      </template>
    </div>
  </div>
</template>

<script>
import { ref, reactive, computed } from "vue";
import { onClickOutside } from "@vueuse/core";
import axios from "axios";
export default {
  name: "XtxCity",
  props: {
    fullLocation: {
      type: Array,
      default: () => [],
    },
  },
  setup(props, { emit }) {
    // 控制展开收起,默认收起
    const active = ref(false);
    const openDialog = () => {
      active.value = true;
    };
    const closeDialog = () => {
      active.value = false;
    };
    // 切换展开收起
    const toggleDialog = async () => {
      if (active.value) closeDialog();
      else {
        cityData.value = await getCityData();
        openDialog();
        // 清空选择结果
        for (const key in changeResult) {
          changeResult[key] = "";
        }
      }
    };
    // 点击其他位置隐藏
    const target = ref(null);
    onClickOutside(target, () => {
      closeDialog();
    });
    // 获取城市数据
    // 1. 数据在哪里？https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json
    // 2. 何时获取？打开城市列表的时候，做个内存中缓存
    // 3. 怎么使用数据？定义计算属性，根据点击的省份城市展示
    const getCityData = async () => {
      if (!window.cityData) {
        // 没有有缓存
        const { data } = await axios(
          "https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json"
        );
        window.cityData = data;
      }
      return window.cityData;
    };
    // 获取城市数据,显示当前地方列表
    // 2. 显示和隐藏函数（为什么是函数，做更多事情）
    const loading = ref(false);
    // 城市数据
    const cityData = ref([]);
    const open = () => {
      visible.value = true;
      loading.value = true;
      // 获取数据
      getCityData().then((data) => {
        cityData.value = data;
        loading.value = false;
      });
      // 清空选择结果
      for (const key in changeResult) {
        changeResult[key] = "";
      }
    };

    const changeResult = reactive({
      provinceCode: "",
      provinceName: "",
      cityCode: "",
      cityName: "",
      countyCode: "",
      countyName: "",
      fullLocation: "",
    });

    const changeItem = (item) => {
        console.log(1);

      // 省份
      if (item.level === 0) {
        changeResult.provinceCode = item.code;
        changeResult.provinceName = item.name;
        console.log(12);
      }
      // 市
      else if (item.level === 1) {
        changeResult.cityCode = item.code;
        changeResult.cityName = item.name;
        console.log(13);
      }
      // 地区
      else if (item.level === 2) {
        changeResult.countyCode = item.code;
        changeResult.countyName = item.name;
        changeResult.fullLocation = `${changeResult.provinceName} ${changeResult.cityName} ${changeResult.countyName}`;
        closeDialog();
        console.log(14);
        emit("change", changeResult);
      }
    };
    // 定义计算属性
    const currList = computed(() => {
      let currList = cityData.value;
      // 城市
      if (changeResult.provinceCode) {
        currList = currList.find(
          (p) => p.code === changeResult.provinceCode
        ).areaList;
      }
      // 地区
      if (changeResult.cityCode) {
        currList = currList.find(
          (c) => c.code === changeResult.cityCode
        ).areaList;
      }
      // TODO 根据点击的省份城市获取对应的列表
      return currList;
    });
    return {
      active,
      toggleDialog,
      target,
      currList,
      loading,
      changeItem,
      cityData,
    };
  },
};
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    .loading {
      height: 290px;
      width: 100%;
      background: url(../../assets/images/loading.gif) no-repeat center;
    }
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
